package com.aiti.lulian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.lulian.dto.LuLianSourcingDemandDto;
import com.aiti.lulian.entity.*;
import com.aiti.lulian.mapper.ILuLianMatterMapper;
import com.aiti.lulian.mapper.ILuLianSourcingContactInfoMapper;
import com.aiti.lulian.mapper.ILuLianSourcingDemandMapper;
import com.aiti.lulian.mapper.ILuLianSourcingPurchaseMapper;
import com.aiti.lulian.service.ILuLianSourcingDemandService;
import com.aiti.lulian.service.ILuLianSourcingPurchaseService;
import com.aiti.lulian.service.ISysAreaService;
import com.aiti.lulian.service.ISysDataService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName LuLianSourcingDemandServiceImpl
 * @Description 供需商机表service实现类
 * @Author diaozhende
 * @Date 2024/5/31 0031 10:49
 */
@Slf4j
@Service
public class LuLianSourcingDemandServiceImpl extends BaseSerivceImpl<ILuLianSourcingDemandMapper, LuLianSourcingDemand> implements ILuLianSourcingDemandService {


    @Resource
    private ILuLianSourcingPurchaseService sourcingPurchaseService;
    @Resource
    private ILuLianMatterMapper matterMapper;
    @Resource
    private ILuLianSourcingPurchaseMapper sourcingPurchaseMapper;
    @Resource
    private ILuLianSourcingContactInfoMapper contactInfoMapper;
    @Resource
    private ISysDataService sysDataService;
    @Resource
    private ISysAreaService sysAreaService;

    /**
     * 分页查询
     *
     * @param sourcingDemandDto 参数对象
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.aiti.lulian.entity.LuLianSourcingDemand>
     * @date 2024/5/31 0031 10:59
     * @author diaozhende
     */
    @Override
    public Page<LuLianSourcingDemand> queryPage(LuLianSourcingDemandDto sourcingDemandDto) {
        Page<LuLianSourcingDemand> page = new Page<LuLianSourcingDemand>(sourcingDemandDto.getPage(), sourcingDemandDto.getPageSize());
        return baseMapper.queryPage(page, sourcingDemandDto);
    }


    /**
     * 根据id查询
     *
     * @param id ID
     * @return com.aiti.lulian.entity.LuLianSourcingDemand
     * @date 2024/6/4 0004 10:40
     * @author diaozhende
     */
    @Override
    public LuLianSourcingDemand queryById(String id,String userId) {
        LuLianSourcingDemand luLianSourcingDemand = baseMapper.queryById(id);
        if (luLianSourcingDemand != null && !BeanUtil.isEmpty(luLianSourcingDemand.getShowSourcingPurchase())) {
            String businessModel = luLianSourcingDemand.getShowSourcingPurchase().getBusinessModel();
            if (StrUtil.isNotEmpty(businessModel)) {
                String[] businessModelList = businessModel.split(",");
                List<SysData> businessModelDataList = sysDataService.list(new QueryWrapper<SysData>()
                        .in("base_id", businessModelList)
                );
                luLianSourcingDemand.setBusinessModelDataList(businessModelDataList);
            }
            String certificate = luLianSourcingDemand.getShowSourcingPurchase().getCertificate();
            if(StrUtil.isNotEmpty(certificate)){
                String[] certificateList = certificate.split(",");
                List<SysData> certificateDataList = sysDataService.list(new QueryWrapper<SysData>()
                        .in("base_id", certificateList)
                );
                luLianSourcingDemand.setCertificateDataList(certificateDataList);
            }
            String supplierRegion = luLianSourcingDemand.getShowSourcingPurchase().getSupplierRegion();
            String regionLabel = "";
            if(StrUtil.isNotEmpty(supplierRegion)) {
                String[] supplierRegionList = supplierRegion.split(",");
                for (String region : supplierRegionList) {
                    SysArea area = sysAreaService.getOne(new QueryWrapper<SysArea>()
                            .eq("base_id", region)
                    );
                    if(!BeanUtil.isEmpty(area)) {
                        regionLabel += " " + area.getBaseName();
                    }
                }
                luLianSourcingDemand.getShowSourcingPurchase().setAddress(regionLabel);
            }
        }
        LuLianSourcingContactInfo sourcingContactInfo = contactInfoMapper.selectOne(new QueryWrapper<LuLianSourcingContactInfo>()
                .eq("sourcing_id", id)
        );
        luLianSourcingDemand.setContactInfo(sourcingContactInfo);
        return luLianSourcingDemand;
    }


    /**
     * 多参数查询
     *
     * @param sourcingDemand 多参数数据
     * @return java.util.List<com.aiti.lulian.entity.LuLianSourcingDemand>
     * @date 2024/6/14 0014 8:58
     * @author diaozhende
     */
    @Override
    public List<LuLianSourcingDemand> queryByParams(LuLianSourcingDemand sourcingDemand) {
        return baseMapper.queryByParams(sourcingDemand);
    }

    @Override
    public void saveData(LuLianSourcingDemand demand) {
        demand.setBaseCreateTime(DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        super.save(demand);
        if (CollectionUtil.isNotEmpty(demand.getMatterList())) {
            demand.getMatterList().forEach(item -> {
                item.setDemandId(demand.getBaseId());
                matterMapper.insert(item);
            });
            demand.getPurchase().setSourcingId(demand.getBaseId());
            sourcingPurchaseMapper.insert(demand.getPurchase());
            demand.getContactInfo().setSourcingId(demand.getBaseId());
            contactInfoMapper.insert(demand.getContactInfo());
        }
    }


    /**
     * 首页新增询价信息
     *
     * @param sourcingDemand 请求参数
     * @return void
     * @date 2024/6/19 0019 15:28
     * @author diaozhende
     */
    @Override
    public void saveDemandData(LuLianSourcingDemand sourcingDemand) {
        sourcingDemand.setBaseCreateTime(DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        super.save(sourcingDemand);
        LuLianSourcingPurchase sourcingPurchase;
        if (BeanUtil.isEmpty(sourcingDemand.getShowSourcingPurchase())) {
            sourcingPurchase = new LuLianSourcingPurchase();
        } else {
            sourcingPurchase = sourcingDemand.getShowSourcingPurchase();
        }
        sourcingPurchase.setSourcingId(sourcingDemand.getBaseId());
        sourcingPurchaseService.save(sourcingPurchase);
    }

    @Override
    public LuLianSourcingDemand queryByBaseId(String baseId) {
        LuLianSourcingDemand demand = getById(baseId);

        List<LuLianMatter> list = matterMapper.getMatterList(baseId);
        demand.setMatterList(list);

        LuLianSourcingPurchase purchase = sourcingPurchaseMapper.selectPurchase(baseId);
        if(purchase == null){
            demand.setShowSourcingPurchase(new LuLianSourcingPurchase());
        }else{
            demand.setShowSourcingPurchase(purchase);
        }
        return demand;
    }

}
